package com.educate.aclservice.controller;


import com.educate.aclservice.entity.AclPermission;
import com.educate.aclservice.service.AclPermissionService;
import com.educate.commonutils.ResultOne;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 权限 前端控制器
 * </p>
 *
 * @author educate.wht
 * @since 2022-09-03
 */
@RestController
@RequestMapping("/admin/acl/permission")
@CrossOrigin
public class AclPermissionController {

    @Autowired
    private AclPermissionService permissionService;

    //获取全部菜单
    @ApiOperation(value = "查询所有菜单")
    @GetMapping
    public ResultOne indexAllPermission() {
        List<AclPermission> list =  permissionService.queryAllMenuGuli();
        return ResultOne.ok().data("children",list);
    }

    @ApiOperation(value = "递归删除菜单")
    @DeleteMapping("remove/{id}")
    public ResultOne remove(@PathVariable String id) {
        permissionService.removeChildByIdGuli(id);
        return ResultOne.ok();
    }

    @ApiOperation(value = "给角色分配权限")
    @PostMapping("/doAssign")
    public ResultOne doAssign(String roleId,String[] permissionId) {
        permissionService.saveRolePermissionRealtionShipGuli(roleId,permissionId);
        return ResultOne.ok();
    }

    @ApiOperation(value = "根据角色获取菜单")
    @GetMapping("toAssign/{roleId}")
    public ResultOne toAssign(@PathVariable String roleId) {
        List<AclPermission> list = permissionService.selectAllMenu(roleId);
        return ResultOne.ok().data("children", list);
    }



    @ApiOperation(value = "新增菜单")
    @PostMapping("save")
    public ResultOne save(@RequestBody AclPermission permission) {
        permissionService.save(permission);
        return ResultOne.ok();
    }

    @ApiOperation(value = "修改菜单")
    @PutMapping("update")
    public ResultOne updateById(@RequestBody AclPermission permission) {
        permissionService.updateById(permission);
        return ResultOne.ok();
    }


}

